program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils;

var
  mass:array  of integer;
  i, m, t, N:integer;

procedure Heapify(num:integer);
var
  x, a, b, d:integer;
begin
    while (1=1) do
    begin
      x:=mass[num];
      a:=2000000000;
      b:=a;
      if (2*num+1<N) then
        a:=mass[2*num+1];
      if (2*num+2<N) then
        b:=mass[2*num+2];
      if (a<=b) and (a<x) then
      begin
        d:=mass[num];
        mass[num]:=mass[2*num+1];
        mass[2*num+1]:=d;
        num:=2*num+1;
      end else
        if (b<=a) and (b<x) then
        begin
          d:=mass[num];
          mass[num]:=mass[2*num+2];
          mass[2*num+2]:=d;
          num:=2*num+2;
        end else
          break;
        end;
  end;

begin
  readln (N);
  m:=N;
  setlength(mass, N);
  for i := 0 to N - 1 do
    read (mass[i]);
  for i := N-1 downto 0 do
    Heapify(i);
  for i := 0 to m- 1 do
  begin
    t:=mass[N-1];
    mass[N-1]:=mass[0];
    mass[0]:=t;
    dec(N);
    Heapify(0);
  end;
  N:=m;
  for i := N-1 downto  0 do
    write (mass[i], ' ');
end.

